package com.wngbms;

import java.io.*;
import java.util.Random;

public class Generate4GNumberFile {

    public static void main(String[] args) throws IOException {
//        m1();
        m1("I:\\number.dat", 64);

        System.out.println("=================");

        m2("I:\\number.dat", 64);

//        M2();
    }

    private static void M2() {
        byte[] buf = new byte[32];
        Random random = new Random();

        for (int i = 0; i < 8; i++) {
//            int i1 = random.nextInt(Integer.MAX_VALUE);
            int i1 = 1023;
            System.out.println(i1);
            int base = i * 4;
            buf[base] = (byte) ((i1 >>> 24) & 0xFF);
            buf[base + 1] = (byte) ((i1 >>> 16) & 0xFF);
            buf[base + 2] = (byte) ((i1 >>> 8) & 0xFF);
            buf[base + 3] = (byte) ((i1) & 0xFF);
        }

        System.out.println("==========================");

        for (int i = 0; i < 8; i++) {
            int base = i * 4;
            int a = (buf[base] << 24 & 0xFF000000)
                    | (buf[base + 1] << 16 & 0xFF0000)
                    | (buf[base + 2] << 8 & 0xFF00)
                    | (buf[base + 3] & 0xFF);
            System.out.println(a);
        }
    }

    private static void m2(String path, int size) throws IOException {
        File f = new File(path);

        try (FileInputStream fis = new FileInputStream(f);) {

            byte[] buf = new byte[size];
            fis.read(buf, 0, size);

            for (int i = 0; i < size / 4; i++) {
                int base = i * 4;
                int a = (buf[base] << 24 & 0xFF000000)
                        | (buf[base + 1] << 16 & 0xFF0000)
                        | (buf[base + 2] << 8 & 0xFF00)
                        | (buf[base + 3] & 0xFF);
                System.out.println(a);
            }
        }
    }

    private static void m1(String path, int size) throws IOException {
        File f = new File(path);

        try (FileOutputStream fos = new FileOutputStream(f);) {
            Random random = new Random();

            byte[] buf = new byte[size]; // 512MB 2 << 29

            for (int i = 0; i < buf.length >> 2; i++) {
                int i1 = random.nextInt(Integer.MAX_VALUE);
                System.out.println(i1);
                int base = i * 4;
                buf[base] = (byte) ((i1 >>> 24) & 0xFF);
                buf[base + 1] = (byte) ((i1 >>> 16) & 0xFF);
                buf[base + 2] = (byte) ((i1 >>> 8) & 0xFF);
                buf[base + 3] = (byte) ((i1) & 0xFF);
            }

            fos.write(buf);
        }
    }

    private static void m1() throws IOException {
        m1("I:\\number.dat", 2 << 29);
    }

}
